package cn.iocoder.yudao.module.shop.controller.admin.giftitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.giftitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftitem.ShopGiftItemDO;
import cn.iocoder.yudao.module.shop.convert.giftitem.ShopGiftItemConvert;
import cn.iocoder.yudao.module.shop.service.giftitem.ShopGiftItemService;

@Tag(name = "管理后台 - 礼品项")
@RestController
@RequestMapping("/shop/gift-item")
@Validated
public class ShopGiftItemController {

    @Resource
    private ShopGiftItemService giftItemService;

    @PostMapping("/create")
    @Operation(summary = "创建礼品项")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:create')")
    public CommonResult<Long> createGiftItem(@Valid @RequestBody ShopGiftItemCreateReqVO createReqVO) {
        return success(giftItemService.createGiftItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼品项")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:update')")
    public CommonResult<Boolean> updateGiftItem(@Valid @RequestBody ShopGiftItemUpdateReqVO updateReqVO) {
        giftItemService.updateGiftItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除礼品项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:gift-item:delete')")
    public CommonResult<Boolean> deleteGiftItem(@RequestParam("id") Long id) {
        giftItemService.deleteGiftItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得礼品项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:query')")
    public CommonResult<ShopGiftItemRespVO> getGiftItem(@RequestParam("id") Long id) {
        ShopGiftItemDO giftItem = giftItemService.getGiftItem(id);
        return success(ShopGiftItemConvert.INSTANCE.convert(giftItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得礼品项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:query')")
    public CommonResult<List<ShopGiftItemRespVO>> getGiftItemList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopGiftItemDO> list = giftItemService.getGiftItemList(ids);
        return success(ShopGiftItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得礼品项分页")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:query')")
    public CommonResult<PageResult<ShopGiftItemRespVO>> getGiftItemPage(@Valid ShopGiftItemPageReqVO pageVO) {
        PageResult<ShopGiftItemDO> pageResult = giftItemService.getGiftItemPage(pageVO);
        return success(ShopGiftItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出礼品项 Excel")
    @PreAuthorize("@ss.hasPermission('shop:gift-item:export')")
    @OperateLog(type = EXPORT)
    public void exportGiftItemExcel(@Valid ShopGiftItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopGiftItemDO> list = giftItemService.getGiftItemList(exportReqVO);
        // 导出 Excel
        List<ShopGiftItemExcelVO> datas = ShopGiftItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "礼品项.xls", "数据", ShopGiftItemExcelVO.class, datas);
    }

}
